package com.resataurant.Controller.User;

import com.resataurant.Entity.Dish;
import com.resataurant.Entity.Order;
import com.resataurant.Service.Impl.OrderServiceImpl;
import com.resataurant.Service.Impl.UserServiceImpl;
import com.resataurant.Service.OrderService;
import com.resataurant.Service.UserService;
import net.sf.json.JSONObject;


import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@WebServlet(name = "BuyDishServlet",urlPatterns = "/BuyDishServlet")
public class BuyDishServlet extends HttpServlet {

    UserService user = new UserServiceImpl();
    OrderService orderService = new OrderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");//防止中文乱码

        String DishID = request.getParameter("id");//获取用户账号
        String UserNote=request.getParameter("UserNote");
        String method = request.getParameter("method");
        switch (method) {
            case "buyOne":
                Order order = new Order();
                Dish dish = user.searchDishById(DishID);
                order.setW_id(dish.getDishId());
                order.setW_Name(dish.getDishName());
                order.setW_Price(dish.getDishPrice());
                order.setW_user_id((String) request.getSession().getAttribute("userid"));
                order.setW_contenct(UserNote);
                int result = orderService.addOrder(order);
                System.out.println("购买结果" + result);
                request.getSession().setAttribute("buy", "1");
                if (result == 0) {
                    //失败
                    request.getSession().setAttribute("buyres", "购买失败");
                    //跳转回登录界面
                    response.sendRedirect("user.jsp");
                } else {
                    //用户
                    request.getSession().setAttribute("buyres", "购买成功");
                    //跳转回登录界面
                    response.sendRedirect("user.jsp");
                }
                break;
            case "buyCartAll":
                HashMap<String, Dish> cart = (HashMap<String, Dish>) request.getSession().getAttribute("cart");
                List<Order> orders=new ArrayList<>();

                for (Dish dish1 : cart.values()) {
                    Order order1 = new Order();
                    int num=Integer.parseInt(request.getParameter(dish1.getDishId()));
                    System.out.println(dish1.getDishId()+"购买数量"+num);
                    order1.setW_id(dish1.getDishId());
                    order1.setW_Name(dish1.getDishName());
                    order1.setW_Price(dish1.getDishPrice());
                    order1.setW_user_id((String) request.getSession().getAttribute("userid"));
                    order1.setW_num(num);
                    orders.add(order1);
                }
                int res=orderService.addCartOrder(orders);
                if(res==0) {
                    //失败
                    request.getSession().setAttribute("buyCartres", "购买失败");
                    //跳转回登录界面
                    response.sendRedirect("CartTest.jsp");
                }else {
                    //用户
                    request.getSession().setAttribute("buyCartres", "购买成功");
                    request.getSession().removeAttribute("cart");
                    //跳转回登录界面
                    response.sendRedirect("CartTest.jsp");
                }
                break;
        }
    }
}
